<template>
	<view class="container">
		<view class="ywyj-body">
		    
			<uni-section title="只能选择一份文件" type="line">
				<view class="example-body">
					<uni-forms ref="form" :model="dataYwyj" label-width="100px" >
						<uni-forms-item label="要件名称" name="fileName">
							<uni-easyinput v-model="dataYwyj.fileName" placeholder="请输入要件名称" />
						</uni-forms-item>
						
						<uni-file-picker 
						file-mediatype="all"
						file-extname="png,jpg,jpeg,pdf" 
						mode="grid" 
						limit="1"
						title="最多选择1份文件"
						@select="select"
						v-model="imageValue"
						></uni-file-picker>
					</uni-forms>
					<button style="margin-top: 20px;" type="primary" @click="submit" >提交</button>
				</view>
			</uni-section>
		</view>
	</view>
</template>

<script>
	import storage from '@/utils/storage'
	import config from '@/config'
	import { getToken } from '@/utils/auth'
	import { addYwyj } from "@/api/trms/trms"
	export default {
		data() {
			return {
				patientId: null,
				uploadUrl: config.baseUrl + '/trms/patient/fileUpload',
				dataYwyj:{
					filePath: '',
					fileName: '',
					patientId: null
				},
				imageValue:[]
			}
		},
		onLoad(option){
			this.patientId = option.patientId;
			console.log("进入附件页面后、参数：",this.patientId)
			if(this.patientId){
				// 查询附件
				
			}
			this.eventChannel = this.getOpenerEventChannel();
			// 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
			this.eventChannel.on('listPageData', function(data) {
				
			});	
		},
		methods: {
			submit(){
				if(!this.dataYwyj.filePath){
					this.$modal.msgError('请选择文件');
					return;
				}
				this.dataYwyj.patientId = this.patientId;
				addYwyj(this.dataYwyj).then(response => {
				  if(response.code == 200){
					  this.$modal.msgSuccess("新增成功");
					  this.backIndex();
					  return;
				  }
				  this.$modal.msgError(response.msg);
				})	
			},
			/**
			 * 回到列表
			 */
			backIndex(){
				let that = this;
				setTimeout(function (){
					// 向起始页通过事件传递数据
					that.eventChannel.emit('listPageData', {data: 'refPage'});
					uni.navigateBack({
						delta: 1
					});
				},500);
			},
			// 获取上传状态
			select(e){
				let that = this;
				console.log('选择文件：',e);
				this.dataYwyj.fileName = e.tempFiles[0].file.name;
				uni.uploadFile({
					url: this.uploadUrl, 
					filePath: e.tempFilePaths[0],//flieList图片等
					name: 'file',//其它上传参数
					header: {
						"Authorization": 'Bearer ' + getToken()
					},
					success: (res) => {
						console.log(res);
						console.log(res.data);
						let data = JSON.parse(res.data);
						if(data.code != 200){
							this.$modal.msgError('文件上传失败，请重试');
							return;
						}
						that.dataYwyj.filePath = data.data.filePath;
					},
					// 获取上传进度
					progress(e){
						console.log('上传进度：',e)
					},
					fail: error => {
						uni.showModal({
							title: '提示',
							content: error,
							showCancel: false
						});
						uni.hideLoading();
					},
					complete: () => {
						uni.hideLoading();
					}
				});
			}
		}
	}
</script>

<style>
	page {
	  background-color: #ffffff;
	}
	
	.example {
	  padding: 15px;
	  background-color: #fff;
	}
	
	.container .ywyj-body .example-body{
		padding: 20px;
	}
</style>
